const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId: '1',
    section: [{
      name: '心得列表',
      typeId: '1'
    }, {
      name: '心得统计',
      typeId: '2'
    } ,
    {
      name: '学分排名',
      typeId: '3'
    }] ,
    current: 'tab2',
    type: '', // 获取类型 1企业读书 2六项精进 3塾生读书
    experienceList: [], // 心得列表
    experienceListCurrentPage: 1, // 心得列表当前页码
    experienceListLastPage: 1, // 心得列表最后一页页码
    experienceSort: {}, // 心得统计
    experienceSortFlag: true, // 心得统计是否为空
    teamRanking: [], // 学分排名
    fontSetSize: 'small', // 用户设置的字体大小（'small'：小，'middle'：中，'large'：大）
  },

  handleChange ({ detail }) {
    this.setData({
      current: detail.key
    });
  },

  handleTap: function(e) {
    let id = e.currentTarget.id;
    if(id){
      this.setData({
        currentId:id
      })
    }
  },

  /**
   * 获取心得列表
   */
  getExperienceList () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/study_list',
        data: {
          token,
          page: this.data.experienceListCurrentPage,
          // type: this.data.type // 获取类型 1企业读书 2六项精进 3塾生读书，不传取全部
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'experienceListRes')
      this.setData({
        experienceList: res.data.data.data,
        experienceListLastPage: res.data.data.data.last_page
      })
    }).catch(err => {
      console.log(err, 'experienceListErr')
    })
  },

  /**
   * 获取塾生学习心得统计
   */
  getExperienceSort () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/my_personal_work',
        data: {
          token
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'experienceSortRes')
      let experienceSortFlag = res.data.data === {} ? true : false
      this.setData({
        experienceSort: res.data.data,
        experienceSortFlag
      })
    }).catch(err => {
      console.log(err, 'experienceSortErr')
    })
  },

  /**
   * 获取学分排名
   */
  getTeamRanking () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/sign_user_order',
        data: {
          token
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'teamRankingRes')
      this.setData({
        teamRanking: res.data.data.data
      })
    }).catch(err => {
      console.log(err, 'teamRankingErr')
    })
  },

  /**
   * 将秒数转换成“分钟：秒数”的格式
   * @param {时间（秒数）} duration 
   */
  timeFormatter(duration) {

    let secondTime = Math.floor(duration); // 秒

    let minuteTime = 0; // 分

    if (secondTime > 60) {

      // 获取分钟，除以60取整数，得到整数分钟
      minuteTime = parseInt(secondTime / 60);

      // 获取秒数，秒数取佘，得到整数秒数
      secondTime = parseInt(secondTime % 60);

    }
    let result = secondTime > 9 ? secondTime : ('0' + secondTime);
    if (minuteTime > 0) {
      result = (minuteTime > 9 ? minuteTime : ('0' + minuteTime)) + ":" + result;
    } else {
      result = "00" + ":" + result;
    }
    return result;
  },
  
  /**
   * 点赞
   * @param {自定义属性} e 
   */
  handleSupport (e) {
    let study_id = e.currentTarget.dataset.study_id
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/create_support',
        data: {
          token,
          study_id
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'supportRes')
      this.getExperienceList()
    }).catch(err => {
      console.log(err, 'supportErr')
    })
  },

  /**
   * 取消点赞
   * @param {自定义属性} e 
   */
  handleDeleteSupport (e) {
    let study_id = e.currentTarget.dataset.study_id
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/del_support',
        data: {
          token,
          study_id
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'DeleteSupportRes')
      this.getExperienceList()
    }).catch(err => {
      console.log(err, 'DeleteSupportErr')
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type
    this.setData({
      type
    })
    this.getExperienceList()
    this.getExperienceSort()
    this.getTeamRanking()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let fontSetSize = wx.getStorageSync('fontSetSize') // 本地缓存中用户设置的字体大小
    if (fontSetSize) {
      this.setData({
        fontSetSize
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let experienceListCurrentPage = this.data.experienceListCurrentPage
    if (experienceListCurrentPage < this.data.experienceListLastPage) {
      experienceListCurrentPage = experienceListCurrentPage + 1
      this.setData({
        experienceListCurrentPage
      })
      this.getExperienceList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})